package com.vogue.supplymall.example.controller;


import com.vogue.gear.memory.provider.IMemProvider;
import com.vogue.ssm.exception.BaseException;
import com.vogue.supplymall.example.domain.model.Member;
import com.vogue.supplymall.example.domain.model.Order;
import com.vogue.supplymall.example.domain.service.MemberService;
import com.vogue.supplymall.example.domain.service.OrderService;
import com.vogue.supplymall.example.exception.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017-11-28.
 * master branch
 */
@RestController
public class TestController extends BaseController{

    @Autowired
    private MemberService memberService;
    @Autowired
    private OrderService orderService;

    @RequestMapping( value = "/insertmember",method = RequestMethod.GET)
    public Member insertmember(HttpServletRequest request){
        Member member=new Member();
        member.setName("功能1");
        memberService.save(member);
        return member;
    }

    @RequestMapping( value = "/insertorder/{id}",method = RequestMethod.GET)
    public String insertorder(@PathVariable Long id, HttpServletRequest request){
        //385116048329801728
        Member member=memberService.get(id);
        Order order1=new Order();
        order1.setGoods("货品一");
        order1.setMember(member);
        orderService.save(order1);
        Order order2=new Order();
        order2.setGoods("货品二");
        order2.setMember(member);
        orderService.save(order2);
        //
        return "success";
    }

    @RequestMapping( value = "/getmember",method = RequestMethod.GET)
    public ResponseEntity<Member> getorders(HttpServletRequest request) throws BaseException {
        //385116048329801728
        Order order=orderService.findById(385130579999326208L);
        HttpStatus status = order != null ? HttpStatus.OK : HttpStatus.NOT_FOUND;
        if(order == null){
            throw new ResourceNotFoundException("member");
        }
        request.getSession().setAttribute("AAAAA","BBBBBB");
        /*if(order == null){
            ErrorMsg error = new ErrorMsg(4 , "");
            return new ResponseEntity<ErrorMsg>(error, status);
        }*/
        return new ResponseEntity<Member>(order.getMember(),status);
    }

    @RequestMapping( value = "/getmember0",method = RequestMethod.GET)
    public ResponseEntity<String> getmember0(HttpServletRequest request) throws BaseException {
        //385116048329801728
        String s=(String) request.getSession().getAttribute("AAAAA");
        /*if(order == null){
            ErrorMsg error = new ErrorMsg(4 , "");
            return new ResponseEntity<ErrorMsg>(error, status);
        }*/
        return new ResponseEntity<String>(s,HttpStatus.OK);
    }

    @RequestMapping( value = "/updatemember",method = RequestMethod.GET)
    public ResponseEntity<Member> updatemember(HttpServletRequest request) {
        Member member=memberService.get(385116048329801728L);
        member=null;
        member.getId();
        memberService.update(member);
        return new ResponseEntity<Member>(member,HttpStatus.OK);
    }


    @Resource
    private IMemProvider<Map> memProvider;

    @RequestMapping( value = "/getvalue",method = RequestMethod.GET)
    public String getValue1(HttpSession session){
        Map data=memProvider.get("AAA");
        return (String) data.get("B");
    }

    @RequestMapping( value = "/setvalue",method = RequestMethod.GET)
    public String setValue1(String value,HttpSession session){
        Map data=new HashMap();
        data.put("B",value);
        memProvider.put("AAA",data);
        return "success";
    }

    @RequestMapping( value = "/modvalue",method = RequestMethod.GET)
    public String modValue1(String value , HttpSession session){
        Map data=memProvider.get("AAA");
        data.put("B",value);
        return "success";
    }
}
